package com.bawei.down;

import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class MainActivity extends AppCompatActivity {
   String moviepath="http://114.112.104.151:8089/videos/2015/08/20/1440065128932/1440065128933.mp4";
    private ProgressBar pb;
   int  mCurrent;
    //安装路径

    File file;
    private File path;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what==1){
                    pb.setMax(msg.arg1);
            }
            if (msg.what==2){
                pb.setProgress(msg.arg1);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        path = Environment.getExternalStorageDirectory();

        setContentView(R.layout.activity_main);
        pb = (ProgressBar) findViewById(R.id.pb);
        MovieThrad movieThrad = new MovieThrad();
        movieThrad.start();
    }

    class MovieThrad extends  Thread{
             public void run(){
                 URL url = null;
                 try {
                     url = new URL(moviepath);
                    HttpURLConnection  coon = (HttpURLConnection) url.openConnection();
                    coon.setRequestMethod("GET");
                     coon.setConnectTimeout(4000);
                     coon.setReadTimeout(4000);
                     int responseCode = coon.getResponseCode();
//                     String path=.getPath()+"/cardImages";
                   String file= path.getPath()+"/hu";
                     int m = coon.getHeaderFieldInt("Content-Length",-1);
                     //设置总进度
                     Message message = Message.obtain();
                     message.what=1;
                     message.arg1=m;
                     handler.sendMessage(message);
                     File file1 = new File(file);
                     file1.mkdir();
                     MainActivity.this.file = new File(file1,"hu.mp4");
                     //判断文件存在不存在
                     if(!MainActivity.this.file.exists()){
                         //创建文件
                         MainActivity.this.file.createNewFile();
                     }else {
                         //如果存在 先删除,后创建
                         MainActivity.this.file.delete();
                         MainActivity.this.file.createNewFile();
                     }
                     FileOutputStream outputStream = new FileOutputStream(MainActivity.this.file);
                     if (responseCode==200){
                         InputStream inputStream = coon.getInputStream();
                         byte[]arr=new byte[1024];
                         int len=-1;
                       while ((len=inputStream.read(arr))!=-1){

                            outputStream.write(arr,0,len);
                           Message message1 = Message.obtain();
                           mCurrent+=arr.length;
                           message1.what=2;
                           message1.arg1=mCurrent;
                           handler.sendMessage(message1);
                       }
                         //关闭输入流
                         inputStream.close();

                         //关闭输出流
                         outputStream.close();
                     }
                 } catch (Exception e) {
                     e.printStackTrace();
                 }

             }
    }
}
